#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{	int index;
				int i;
				int *next;
				int value;
  	 		};
			   	struct node *start, *rear, *ptr, *temp, *prev, *ptr2,*ptr3, *start2, *rear2;
int in=0;

void inputlist(int n)
{	int x;
	printf("Enter the list elements  seperated by a  space: ");
	while(in!=n)
	{	scanf("%d",&x);		
		ptr=malloc(sizeof(struct node));
		ptr->index=in;
		ptr->value=x;
		ptr->next=NULL;
		if(start==NULL)
	{	
			start=rear=ptr;
	}
		else
		{
			rear->next=ptr;
			rear=ptr;
		}
		in++;
	}
in=in-1;
}

void display(struct node * begin)
{
	ptr=begin;
	printf("\nList is : [");
	while(ptr!=NULL)
	{	printf("%d ",ptr->value);
		ptr=ptr->next;
	}
	printf("]\n");
}

void append()
{
	int number;
	printf("enter the no. u wanna add : ");
	scanf("%d",&number);
	printf("gadhgdvjugsa");
	ptr2=malloc(sizeof(struct node));
	ptr2->value=number;
	ptr2->next=NULL;
	if(start==NULL)
	{
		start = rear =ptr2;
		rear->index=++in;
	}
	else
	{
		ptr = rear;
		rear->next=ptr2;
		rear=ptr2;
		rear->index=++in;
		//in=rear->index;
	}
	start=start;
	display(start);
}

void extend()
{	int num;
	int x;
	int i=0;
	printf("enter the size of new list : ");
	scanf("\n%d",&num);
	printf("Enter the list elements seperated by a space: ");
	start2=NULL;
	rear2=NULL;
	while(i!=num)
	{	scanf("%d",&x);
		ptr3=malloc(sizeof(struct node));
		ptr3->index=++in;
		ptr3->value=x;
		ptr3->next=NULL;
		if(start2==NULL)
		{
			start2=rear2=ptr3;
		}
		else
		{
			rear2->next=ptr3;
			rear2=ptr3;
		}
		//in++;
		i++;
	}
	temp=rear;
	temp->next=start2;
	rear=rear2;
	display(start);
}

void count()
{
	int cnt=0;
	int number;
	printf("\nEnter the no. u wanna count : ");
	scanf("%d",&number);
	ptr=start;
	while(ptr->next!=NULL)
	{
		if(ptr->value==number)
		cnt++;
		ptr=ptr->next;		
	}
	if(rear->value==number)
	cnt++;
	if(cnt>0)
	printf("the no. of terms of this no is : %d",cnt);
	else
	printf("this no. is not present.");
}

void remove2()
{
	int nbr;
	printf("Enter the no. u wanna remove : ");
	scanf("%d",&nbr);
	for(ptr=start,prev=NULL;ptr!=NULL;prev=ptr,ptr=ptr->next)
	{
		if(ptr->value==nbr)
		{	if(ptr==start)
			{temp=ptr->next;
			start=ptr->next;
			ptr=start;
			}
			else{temp=ptr->next;
			prev->next=ptr->next;
			ptr=prev;}
			break;
		}
	}
		if(rear->value==nbr)
		{
			prev->next=NULL;
		}
		else
		{
			printf("No. is not in d list.");
		}
	display(start);
}

void pop()
{
	int input;
	printf("enter the index of the no wanna remove : ");
	scanf("%d",&input);
	for(ptr=start,prev=NULL;ptr!=NULL;prev=ptr,ptr=ptr->next)
	{
		if(ptr->index==input)
		{	if(input==0)
			{	start=ptr->next;
				temp=ptr->next;
				break;
			}
			temp=ptr->next;
			prev->next=ptr->next;
			break;
		}
	}
	display(start);
	for(ptr=temp;ptr!=NULL;ptr=ptr->next)
	{	ptr->index=ptr->index-1;
	}
}

void insert1()
{
	int pos,val;
	printf("Enter the value of the no u wanna insert : ");
	scanf("%d",&val);
	printf("Enter the index where u wanna insert : ");
	scanf("%d",&pos);
	//temp=NULL;
	temp=malloc(sizeof(struct node));
	for(ptr=start,prev=NULL;ptr!=NULL;prev=ptr,ptr=ptr->next)
	{
		if((ptr->index)==pos)
		{	
			
			temp->value=val;
			temp->index=ptr->index;
			temp->next=ptr;
			prev->next=temp;
			break;		
		}
	}
		//prev->next=ptr;
			for(ptr=temp;ptr!=NULL;ptr=ptr->next)
			{
				ptr->index=ptr->index+1;
			} in=in+1;
			
	display(start);
}

void sort()
{
	int tempstore;
	struct node *prev;
	ptr=start;
	for(prev=ptr;prev->next!=NULL;prev=prev->next)
	{	
		for(ptr=prev->next;ptr!=NULL;ptr=ptr->next)
		{	
			if(prev->value > ptr->value)
			{	tempstore=ptr->value;
				ptr->value=prev->value;
				prev->value=tempstore;
			}
		}
	}
	display(start);
}

void indexing(int in)
{
	int n1;
	printf("\nEnter the index: ");
	scanf("\n%d",&n1);
	if(n1<0)
		n1=in+n1+1;
	if(n1>in||n1<0)
	{	
		printf("\nNo such index...");
		return;
	}
	printf("\nRequired Element: ");
	ptr=start;
	while(ptr!=NULL)
	{
		if(ptr->index==n1)
		{	printf("%d",ptr->value);
			break;
		}
		ptr=ptr->next;
	}
}

void reverse()
{ 
	for(ptr=start,prev=NULL,rear=start;ptr!=NULL;prev=ptr,ptr=temp)
	{
		if(ptr->next==NULL)
		{
    		ptr->next=prev;
			temp=NULL;
			start=ptr;
		}
		else
		{
			temp=ptr->next;
			ptr->next=prev;

		}
	}
	ptr=start;
	display(start);
	for(ptr=start;ptr!=NULL;ptr=ptr->next)
	{
		ptr->index=in-ptr->index;
	}

}


void main()
{
	int n;
	int m;
	char more;
	start=NULL;
	rear=NULL;
	printf("Enter the number of elements in the list: ");
	scanf("%d",&n);
	inputlist(n);
	display(start);
do
	{	printf("\n1-Indexing\n2-Count\n3-Reversal\n4-Append\n5-Extend\n6-Pop\n7-Remove\n8-Sort\n9-Insert ");
		printf("\n\nEnter the list operation: ");
		scanf("\n%d",&m);
		switch(m)
		{	case 1:  indexing(in);
  						break;
			case 2:  count();
						break;
			
			case 3:  reverse();
						break;
			case 4: append();
			break;
			case 5: extend();
			break;
			case 6: pop();
			break;
			case 7: remove2();
			break;
			case 8: sort();
			break;
			case 9: insert1();
			break;

		}
		printf("\nMore operations?(y/n): ");
		scanf("\n%c",&more);
	}
	while(more=='y'||more=='Y');
	getchar();
}


